package com.five.wisdomClass.config;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 传递集合实现分页的配置文件
 */
@Configuration
public class PaginationUtil {
    public static <T> IPage<T> paginate(List<T> list, int currentPage, int pageSize) {
        // 创建一个分页对象
        Page<T> page = new Page<>(currentPage, pageSize);

        // 计算分页数据
        int total = list.size();
        int start = (currentPage - 1) * pageSize;
        int end = Math.min(start + pageSize, total);
        List<T> pageList = list.subList(start, end);

        // 设置分页数据
        page.setRecords(pageList);
        page.setTotal(total);

        return page;
    }
}
